<?php

//判断来路跳转
/*
if(strpos($_SERVER['HTTP_REFERER'], '.cn/')){
	header("Location: http://www.chunsir.com/play_{$_GET['iid']}.html"); 
	die();
}
*/
/*
 获取优酷视频地址
 */
class YoukuFlv{
	static private $error   =   "";
	static private $result  =   array();
	//获取相关视频
	static public function getYoukuAbouts($num_iid){
		$about = 'https://openapi.youku.com/v2/videos/by_related.json?client_id=4c44857ddbbad646&video_id='.$num_iid.'&count=20';
		$txt = self::get_curl_contents($about);

		$abouts = json_decode($txt,true);
		$items =array();
		$i=0;
		foreach($abouts['videos'] as $v){
			$items[$i]['num_iid'] =  $v['id'];
			$items[$i]['seo_iid'] =  self::base32_encode($v['id']);
			
			$items[$i]['title'] =  $v['title'];
			$items[$i]['pic_url'] =  $v['thumbnail'];
			$items[$i]['seopic'] =  '/img/' . preg_replace('%http://g[0-9]+.ykimg.com/%', '', $v['thumbnail']) . '.jpg';
			$items[$i]['vtime'] =   (int)(($v['duration']) / 60). '分钟';
			$items[$i]['cat'] = $v['category'];
			$items[$i]['publish'] = $v['published'];
			$i++;
		}
		return $items;
	}
	//获取播放地址和title信息
	static public function getYoukuFlv($iid){
		$item =array();
		$content    =   self::get_curl_contents( "http://v.youku.com/player/getPlayList/VideoIDS/".$iid );
		$data   =   json_decode($content);
		//print_r($data);
		if(!isset($data->data[0]->streamfileids)){
			self::$error    =   "Cannot find this video";
			return false;
		}
		$item['title']= $data -> data[0] -> title ;
		$item['pic_url']=$data -> data[0] -> logo ;
		$items['seopic'] =  '/img/' . preg_replace('%http://g[0-9]+.ykimg.com/%', '', $item['pic_url']) . '.jpg';
		$item['tags']=implode(',',$data -> data[0] -> tags);

		$jj=0;
		$ggflv="";
		$playlist_k="";
		foreach($data->data[0]->segs->flv AS $key){
			$fileid =   self::getfileid( $data->data[0]->streamfileids->flv,$data->data[0]->seed);
			if($jj<10) $dl="0".$jj;
			if($jj==10) $dl="0A";
			if($jj==11) $dl="0B";
			if($jj==12) $dl="0C";
			if($jj==13) $dl="0D";
			if($jj==14) $dl="0E";
			if($jj==15) $dl="0F";
			if($jj>15)  $dl="1".($jj-16);
			$fileid=substr_replace($fileid,$dl,8,2);
			$ggflv=$ggflv." var gggflv;playlist_f['flv']['".$jj."']='".$fileid."'; playlist_k['flv']['".$jj."']='".$key->k."';";
			$jj++;
		}
		$result['playlist']=$ggflv;
		$result['item']=$item;
		return $result;
	}
	static public function error(){
		return self::$error;
	}

	static public function result(){
		return self::$result;
	}
	//远程抓取
	public static function get_curl_contents($url, $second = 3){
		if(!function_exists('curl_init')) die('php.ini未开启php_curl.dll');
		$c = curl_init();
		curl_setopt($c,CURLOPT_URL,$url);
		$UserAgent=$_SERVER['HTTP_USER_AGENT'];
		curl_setopt($c,CURLOPT_USERAGENT,$UserAgent);
		curl_setopt($c,CURLOPT_HEADER,0);
		curl_setopt($c,CURLOPT_TIMEOUT,$second);
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($c,CURLOPT_RETURNTRANSFER, true);
		$cnt = curl_exec($c);
		curl_close($c);
		return $cnt;
	}
	//base32加密
	static private function base32_encode($input) {
		$input = iconv('UTF-8','GB2312//TRANSLIT',$input);
		$BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567';
		$output = '';
		$v = 0;
		$vbits = 0;
		for ($i = 0, $j = strlen($input); $i < $j; $i++) {
			$v <<= 8;
			$v += ord($input[$i]);
			$vbits += 8;
			while ($vbits >= 5) {
				$vbits -= 5;
				$output .= $BASE32_ALPHABET[$v >> $vbits];
				$v &= ((1 << $vbits) - 1);
			}
		}
		if ($vbits > 0) {
			$v <<= (5-$vbits);
			$output .= $BASE32_ALPHABET[$v];
		}
		$output=iconv('GB2312', 'UTF-8', $output);
		return $output;
	}
	//base32解密
	static public function base32_decode($input) {
		$input = iconv('UTF-8','GB2312//TRANSLIT',$input);
		$output = '';
		$v = 0;
		$vbits = 0;
		for($i = 0, $j = strlen($input); $i < $j; $i++) {
			$v <<= 5;
			if ($input[$i] >= 'a' && $input[$i] <= 'z') {
				$v += (ord($input[$i]) - 97);
			}
			elseif ($input[$i] >= '2' && $input[$i] <= '7') {
				$v += (24 + $input[$i]);
			}
			else {
				exit(1);
			}
			$vbits += 5;
			while($vbits >= 8){
				$vbits -= 8;
				$output .= chr($v >> $vbits);
				$v &= ((1 << $vbits) - 1);
			}
		}
		$output=iconv('GB2312', 'UTF-8', $output);
		return $output;
	}
	static private function getSid() {
		$sid = time().(rand(0,9000)+10000);
		return $sid;
	}
	static private function getfileid($fileId,$seed) {
		$mixed = self::getMixString($seed);
		$ids = explode("*",$fileId);
		unset($ids[count($ids)-1]);
		$realId = "";
		for ($i=0;$i < count($ids);++$i) {
			$idx = $ids[$i];
			$realId .= substr($mixed,$idx,1);

		}
		return $realId;
	}
	static private function getMixString($seed) {
		$mixed = "";
		$source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
		$len = strlen($source);
		for($i=0;$i< $len;++$i){
			$seed = ($seed * 211 + 30031) % 65536;
			$index = ($seed / 65536 * strlen($source));
			$c = substr($source,$index,1);
			$mixed .= $c;
			$source = str_replace($c, "",$source);
		}
		return $mixed;
	}
}



$num_iid = YoukuFlv::base32_decode($_GET['iid']);
$abouts = YoukuFlv::getYoukuAbouts($num_iid);
$res = YoukuFlv::getYoukuFlv($num_iid);
$playlist = $res['playlist'];
$item = $res['item'];

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>《<?php echo $item['title'];?>》 正在播放 - <?php echo $item['title'];?>播放地址</title>
<script type="text/javascript" src="/play/js/offlights.js"></script>
<script type="text/javascript">var playlist_f={};var c;playlist_f['mp4']={};playlist_f['flv']={};playlist_f['hd2']={};var playlist_k={};playlist_k['mp4']={};playlist_k['flv']={};playlist_k['hd2']={};ep='MAXQTA4eIrrZ2PnJ8uJxVYb0sBc81w\/KWxo=';oip='3702884569';<?php echo $playlist;?></script>
<script type="text/javascript" src="/play/play.js"></script>  
<link href="/static/video/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="die_header">
  <div class="head">
    <div class="logo"><img src="/static/video/images/logo.png" width="202" height="50" alt="LOGO" /></div>
    <div class="search">
      <form name="formsearch" id="formsearch" action='/search.jsp' method="post" target="search_window">
        <input class="keyword" name="searchword" value="请输入影片名或演员名" onClick="if(this.value=='请输入影片名或演员名')this.value=''" onBlur="if(this.value=='')this.value='请输入影片名或演员名'" type="text" id="searchword"/>
        <input type="submit" value="搜索" class="look"/>
      </form>
      <div class="hotkey"></div>
    </div>
    <div class="query"> <a href="javascript:void(0)" onClick="addFavorite('http://{$SITE_URL}','{$index_title}');" class="tt" rel="nofollow">加入收藏</a> <a href="/desktop.php" class="tt" rel="nofollow">保存到桌面</a> </div>
  </div>
<script src="/static/video/js/miniad.js"></script>
</div>


<div id="die_wrap">

  <div id="player">
    <div class="die960">
      <div class="die_left">
        <div class="playback">
		<?php
		if(false){
			echo'			<div id="video" style="margin:5px;"><div id="a1"></div></div>
			<script type="text/javascript" src="/play/ckplayer/ckplayer.js" charset="utf-8"></script>
			<script type="text/javascript">var video=["http://movie.ks.js.cn/flv/other/1_0.mp4->video/mp4","http://www.ckplayer.com/webm/0.webm->video/webm","http://www.ckplayer.com/webm/0.ogv->video/ogg"];var support=["iPad","iPhone","ios","android+false","msie10+false"];CKobject.embedHTML5("video","ckplayer_a1",640,500,video,flashvars,support);</script>
			';
		}else{
			echo'<embed type="application/x-shockwave-flash" width="640" height="500" src="http://player.youku.com/player.php/sid/'.$num_iid.'==/v.swf?VideoIDS='.$num_iid.'==&isAutoPlay=true&isShowRelatedVideo=false&showAd=0&embedid=200707011235108b444fd5fae5112597a31314df8885f7" wmode="transparent" isautoplay="true" loop="true"></embed>';
		}
		?>
      </div>
    </div><!--left end-->
	
	<div class="die_right">
        <div class="play_ad300_1">
          300x300
        </div>
        <div class="play_ad300_2">
          300x300
        </div>
      </div><!--right end-->
  </div>

</div><!--play div end-->
  <?php
if(count($abouts)>0){
	echo   '<div class="die960"><div class="record"><div class="h_top"><h1>'.$item['title'].'</h1></div><div class="back"></div><ul class="ul_record" id="videos">';
	foreach($abouts as $v){
		echo "<li><a href='/play_{$v['seo_iid']}.html' class='img' title='{$v['title']}' rel='nofollow'> <img src='{$v['seopic']}' width='165' height='124' /> <label class='bg'></label></a><h3>{$v['title']}</h3><p class='role'></p></li>";
	}
	echo '</ul><div class="back"></div></div></div>';
}?>
</div>

<div id="die_footer">
  <div class="foot_back"></div>
  <div class="footer">
    <p>免责声明:本站所有视频均来自互联网收集而来，版权归原创者所有，如果侵犯了你的权益，请通知我们，我们会及时删除侵权内容，谢谢合作！<br>
      Copyright @2003-2014 <?php echo $item['title'];?> 版权所有 [<a href ='/sitemap.html'>网站地图</a>] [<script src="http://s19.cnzz.com/stat.php?id=5929437&web_id=5929437" language="JavaScript"></script>]</p>
    <p>本站不承担任何由于内容的合法性及健康性所引起的争议和法律责任</p>
  </div>
</div>
</body>
</html>
